package com.example.promethues.action;

import com.example.promethues.dto.PromApiResult;
import com.example.promethues.dto.PromMatrixDataResult;
import com.example.promethues.dto.PromVectorDataResult;
import com.example.promethues.strategy.PromConvertStrategy;
import com.example.promethues.strategy.PromMatrixConvertStrategy;
import com.example.promethues.strategy.PromVectorConvertStrategy;
import com.example.promethues.vo.DatasetVo;
import com.example.promethues.vo.TSVo;
import com.google.gson.Gson;

import com.google.gson.JsonArray;
import com.google.gson.reflect.TypeToken;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

@Controller
public class PromethuesAction {


    @RequestMapping("ts-data")
    @ResponseBody
    public List<DatasetVo> getTSData() {
        String path = "query_range.json";
        String jsonStr = readJsonFile(path);

        PromApiResult promApiResult = new Gson().fromJson(jsonStr, PromApiResult.class);
        System.out.println("promApiResult = " + promApiResult);
        List<DatasetVo> datasetVos=getDatasetVos(promApiResult);

        return datasetVos;
    }

    private List<DatasetVo> getDatasetVos(PromApiResult promApiResult) {
        String resultType = promApiResult.getData().getResultType();
        PromConvertStrategy promConvertStrategy=PromConvertStrategy.create(resultType);
        return promConvertStrategy.toDataSets(promApiResult.getData());
    }

    private String readJsonFile(String path) {
        BufferedReader br = null;
        StringBuilder sb = new StringBuilder();
        try {
            br = new BufferedReader(new InputStreamReader(this.getClass().getClassLoader().getResourceAsStream(path)));
            String line = br.readLine();

            while (line != null) {
                sb.append(line + "\r\n");
                line = br.readLine();
            }
            System.out.println(sb.toString());
        } catch (IOException e) {
            e.printStackTrace();
        }
        return sb.toString();
    }
}
